//
//  WBHomeTableViewCell.swift
//  SainMicroblog
//
//  Created by chenxinju on 16/5/6.
//  Copyright © 2016年 chenxinju-a2. All rights reserved.
//

import UIKit
import SnapKit
class WBHomeTableViewCell: UITableViewCell {
    
    //约束重用属性 
    var bottomConstraint: Constraint?
    
    
    //定义拥有模型属性类
    var statusViewModel: WBStatusViewModel?{
        
        //在此重写给属性赋值方法拦截
        didSet {
          //原创微博视图View
         orginalView.orginalViewModel = statusViewModel
          //评论条
         footerView.footerViewModel = statusViewModel
            
            
        //把重用的约束先删除
        bottomConstraint?.uninstall()
            
        //根据retweeted_status 是否有值来判断是否是转发微博还是原创微博类设置约束
            if statusViewModel?.status?.retweeted_status == nil {
             //print("原创微博")
             reteetView.hidden = true //隐藏
              //更新底部评论条的约束
         footerView.snp_updateConstraints(closure: { (make) -> Void in
              
            bottomConstraint = make.top.equalTo(orginalView.snp_bottom).constraint
             })
            
            }else {
                //print("转发微博")
                reteetView.hidden = false
                footerView.snp_updateConstraints(closure: { (make) -> Void in
            //更新顶部约束为转发微博的底部
         bottomConstraint = make.top.equalTo(reteetView.snp_bottom).constraint
                })
                //设置转发微博数据
                reteetView.retweetViewModel = statusViewModel
            }
         
//           print("原创微博配图数量\(statusViewModel?.status?.pic_urls?.count)")
//            
//            print("转发微博配图数量:\(statusViewModel?.status?.pic_urls?.count)")
            
        }
    }
    

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
        
    }
     //系统加载xib要提示实现的方法
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
    //加载子控件 添加cell头像微博信息到ContenView中
  private  func setupUI() {
     //把源生微博布局类添加
     contentView.addSubview(orginalView)
     //把评论的按钮显示条添加
     contentView.addSubview(footerView)
    //转发微博
     contentView.addSubview(reteetView)
     
        //设置约束 填充整个contenView
        orginalView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.top.equalTo(contentView.snp_top)
            //make.bottom.equalTo(contentView.snp_bottom)
          }
    
///  转发微博
     reteetView.snp_makeConstraints { (make) -> Void in
       make.left.equalTo(contentView.snp_left)
        //注意是顶部是原创微博的底部
        make.top.equalTo(orginalView.snp_bottom)
        make.right.equalTo(contentView.snp_right)
    }
    
///  评论条
        footerView.snp_makeConstraints { (make) -> Void in
         make.left.equalTo(contentView.snp_left)
            //修改顶部约束为转发微博的底部
         bottomConstraint = make.top.equalTo(reteetView.snp_bottom).constraint
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(44)
       }
    
///  自动计算行高 需要计算contentView的边界
      contentView.snp_makeConstraints { (make) -> Void in
       make.top.equalTo(self.snp_top)
        make.left.equalTo(self.snp_left)
        make.right.equalTo(self.snp_right)
        make.bottom.equalTo(footerView.snp_bottom)
    }
    
        
    }
    

    //MARK:-- 懒加载控件
     //原创微博
    private lazy var orginalView: WBOrginalView = WBOrginalView()
    //评论点击条
    private lazy var footerView: WBFooterView = WBFooterView()
    //转发微博
    private lazy var reteetView:WBReteetView = WBReteetView()
    
    
}
